class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) 
    {
        int len = nums.size();
        int ret1 = -1, ret2 = -1;
        if(!len) return {-1, -1};

        // 初始化
        int left = 0, right = len - 1, mid;

        // 求起始位置
        while(left < right)
        {
            mid = (left + right) / 2;
            if(nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        if(nums[right] != target) return {-1, -1};
        ret1 = right;

        // 再次初始化
        left = 0, right = len - 1;
        // 求终止位置
        while(left < right)
        {
            mid = (left + right + 1) / 2;
            if(nums[mid] <= target) left = mid;
            else right = mid - 1;
        }
        if(nums[right] != target) return {-1, -1};
        ret2 = right;

        return {ret1, ret2};
    }
};
